package com.epam.dao.h2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.epam.dao.UserDao;
import com.epam.dao.h2.exceptions.MethodNotDefinedExseption;
import com.epam.database.ConnectionPool;
import com.epam.entity.User;
import com.epam.entity.Role;

public class H2UserDao extends H2AbstractEntityDao <User> implements UserDao<User> {
	private static Logger LOGGER = LoggerFactory.getLogger(H2UserDao.class);
	
	private final static String SELECT_BY_ID = "Select * from users where user_id=?";
	private final static String SELECT_BY_NAME_AND_PASSWORD = 
			"Select user_id, user_name, person_id, role, password from users where user_name=? and password=?";
	
		
	@Override
	public User selectById(long id) {
		User user = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		Role role = null;
		try {
			conn = ConnectionPool.getInstance().takeConnection();
			pst = conn.prepareStatement(SELECT_BY_ID);
			pst.setLong(1, id);
			rs = pst.executeQuery(); LOGGER.info(rs.getString("role"));
			if (rs.next()) {
				if (rs.getString("role").equalsIgnoreCase("S"))
					role = Role.valueOf("STUDENT");				
				if (rs.getString("role").equalsIgnoreCase("T"))
					role = Role.valueOf("TEACHER");
				if (rs.getString("role").equalsIgnoreCase("A"))
					role = Role.valueOf("ADMINISTRATOR");
				user = new User(id, rs.getString("user_name"),
						rs.getString("password"), role);
			}
		} catch (Exception e) {
			LOGGER.error("SELECT_BY_ID not execute");
		}finally {
			try {
				if (rs != null)
					rs.close();
				if (pst != null)
					pst.close();
				if (conn != null) {
					
					ConnectionPool.getInstance().returnConnection(conn);
				}
			} catch (SQLException e) {
				LOGGER.error("Error  closing request SelectUserById");
			}
		}
		return user;
	}

	@Override
	public User selectByName(String username, String password) {
		User user = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		Role role = null;
		try {
			conn = ConnectionPool.getInstance().takeConnection();			
			pst = conn.prepareStatement(SELECT_BY_NAME_AND_PASSWORD);			
			pst.setString(1, username);
			pst.setString(2, password);
			rs = pst.executeQuery();
			
			if (rs.next()) {
				if (rs.getString("ROLE").equalsIgnoreCase("S"))
					role = Role.valueOf("STUDENT");
				if (rs.getString("ROLE").equalsIgnoreCase("T"))
					role = Role.valueOf("TEACHER");
				if (rs.getString("ROLE").equalsIgnoreCase("A"))
					role = Role.valueOf("ADMINISTRATOR");
				user = new User(rs.getLong("user_id"),
						rs.getString("user_name"), rs.getString("password"),
						role);
				LOGGER.info(user.toString());
			}
		} catch (SQLException e) {
			LOGGER.error(e.getMessage());
		}
		catch (Exception e) {
			LOGGER.error("SELECT_BY_NAME_AND_PASSWORD not execute");
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pst != null)
					pst.close();
				if (conn != null) {
					
					ConnectionPool.getInstance().returnConnection(conn);
				}
			} catch (SQLException e) {
				LOGGER.error("Error  closing request SelectUserByNameAndPassword");
			}
		}
		return user;
	}

	@Override
	public List<User> selectAll() {
		try {
			throw new MethodNotDefinedExseption();
		} catch (MethodNotDefinedExseption e) {
			LOGGER.error("Method SelectAll for User not define");
		}
		return null;
	}

	@Override
	public void saveOrUpdate(User entity) {
		try {
			throw new MethodNotDefinedExseption();
		} catch (MethodNotDefinedExseption e) {
			LOGGER.error("Method saveOrUpDate for User not define");
		}
		super.saveOrUpdate(entity);
	}

	@Override
	public boolean delete(long id) {
		try {
			throw new MethodNotDefinedExseption();
		} catch (MethodNotDefinedExseption e) {
			LOGGER.error("Method delete for User not define");
		}
		return false;
	}

	@Override
	public int insert(long id) {
		try {
			throw new MethodNotDefinedExseption();
		} catch (MethodNotDefinedExseption e) {
			LOGGER.error("Method insert for User not define");
		}
		return 0;
	}
}
